<?php
/**
 * 解析第三方产品 为本地产品
 * createtime:2017/08/12
 * updatetime:2017/08/22
 *  * Created by wzb.
 */
class AnalyProduct
{
   public $db;
   public $DB;
   function __construct(){
    $this->DB=new DB();
  }

    /**
     * [Getpruductdetial  获取 订单产品的详细信息]
     * @param [type] $sku             [description]
     * @param [type] $options_id      [description]
     * @param [type] $increment_id    [description]
     * @param [type] $pice_or_lotnums [description]
     * @param [type] $store_id        [description]
     * @param [type] $item_id         [description]
     * @param [type] $item_price      [description]
     * @return bool
     */
public function Getpruductdetial($sku,$options_id,$increment_id,$pice_or_lotnums,$store_id,$item_id,$item_price,$db){
    $this->db=$db;
      //定义数组
      $zuzhuangarrary = array();
      $zuzhuangarrary['increment_id'] = $increment_id;
     // $zuzhuangarrary['amount'] = $item_price;//子订单金额
      //通过组装的产品标示 storeid_sku 获取产品
      $storeid_sku = $store_id.'-'.$sku;
      print_r("sku:".$storeid_sku);
     echo "||||||||||||||||||||||||---from localhost sync data---|||||||||||||||||||||||\n";
     $product_info = "select * from dlz_product_attr_$store_id where storeid_sku='$storeid_sku' order by id desc";
      // $product_info = "select * from dlz_product_attr where storeid_sku='$storeid_sku'";
      $query_product = $this->db->query($product_info);//返回影响了多少行数据
      $rows_product = $query_product->fetch();
            $zuzhuangarrary['product_id'] = $rows_product['product_id'];//产品id
            $category = $rows_product['attribute_set_id'];//类目
            //$zuzhuangarrary['category'] = $category;
            $price = $rows_product['price'];//产品基价
            //$amount = "$item_price" - "$price";
            $zuzhuangarrary['amount'] = $price;//子订单金额
            $zuzhuangarrary['productCount'] = $pice_or_lotnums;//数量
            $zuzhuangarrary['status'] = $rows_product['status'];//产品状态
            $zuzhuangarrary['created_at'] = $rows_product['created_at'];//产品上传时间
            $zuzhuangarrary['updated_at'] = $rows_product['updated_at'];//产品修改时间
            $zuzhuangarrary['weight'] = $rows_product['weight'];//产品重量
            $zuzhuangarrary['color'] = $rows_product['color'];//颜色 
            $zuzhuangarrary['grade'] = $rows_product['grade'];//等级
            $zuzhuangarrary['texture'] = $rows_product['texture'];//花型
            $zuzhuangarrary['length_option_id'] = $rows_product['length_option_id'];//处理3+1 
            $zuzhuangarrary['laceSize'] = $rows_product['lace_size'];//lace尺寸
            //$zuzhuangarrary['density'] = $rows_product['density'];//密度
            $zuzhuangarrary['capSize'] = $rows_product['size'];//大小
            $zuzhuangarrary['memo'] = $rows_product['wig_type'];//备注
            $items_package = $rows_product['items_package'];//打包方式
            $country_type_value = $rows_product['country_type'];//国家

              //处理3+1 新的格式解析
          $Length = $rows_product['length_option_id'];
            /***重要的地方尺寸处理***/
            //WIG  Stretched_inc_option_id
                  //解析json key=option_id  value=option_value
          $option_arr = json_decode($options_id);
          echo "$options_id\n";
          $firstproduct = array();
          //解析是否是多条3+1 新组合
           print_r($Length);
           if (!empty($Length)) {
              echo "：是新品3+1 组合\n";
              $isDuotiao = true;
              $Closure = $Length;
              $Stretched = null;
           }else{
             echo "：不是新品3+1 组合\n";
               $isDuotiao = false;
              $Closure = $rows_product['Closure_inch_option_id'];
              $Stretched = $rows_product['Stretched_inc_option_id'];               
           }
           $part_design = $rows_product['part_design_option_id'];
          foreach ($option_arr as $optionkey => $optionvalue) {
            //产品一 
            
            if (empty($Closure) && empty($Stretched)) {
              echo "Closure:$Closure.Stretched:$Stretched.数据不完整，跳出\n";
             continue;
            }
            $country_type = $rows_product['country_type_option_id'];
            $density = $rows_product['density_option_id'];//2017/11/1
            $color = $rows_product['color_option_id'];//2019/04/13
            $storeid_sku_value = $store_id.'-'.$sku.'-'.$optionvalue;
            echo "< Closure: $Closure   Stretched:$Stretched    $storeid_sku_value        $optionkey>\n";
            //判定订单产品中的option 与  已同步产品中的option ，存在则找到相关信息
            if ($Closure == $optionkey) {
              if (!$isDuotiao) {
                 $firstproduct['P1']['closure'] = $this->getoptionvalue($table_="dlz_product_closureinch",$storeid_sku_value);
              }else{
                 $duotiaolength = $this->getoptionvalue($table_="dlz_product_length",$storeid_sku_value);
                 $duo_inc_arr = explode('&',$duotiaolength);//是否是3+1 拆分
                 $str_duo = $duo_inc_arr[1];//获取字符串
                 $recode_inc = $this->findNum( $str_duo);//提取数字
                 $firstproduct['P1']['closure'] = $recode_inc;
              }
              //$lotNum = '1';
              $firstproduct['P1']['lotNum'] = '1';//打包条数
            }
            if ($Stretched == $optionkey) {
              $firstproduct['P1']['Stretched'] = $this->getoptionvalue($table_="dlz_product_stretchedinc",$storeid_sku_value);
              $firstproduct['P1']['lotNum'] = '1';//打包条数
            }
            if ($part_design == $optionkey) {
              $firstproduct['P1']['hairDesign'] = $this->getoptionvalue($table_="dlz_product_partdesign",$storeid_sku_value);
            }
            if ($country_type == $optionkey && (!empty($Closure) || !empty($Stretched))) {
              $firstproduct['P1']['type_country'] = $this->getoptionvalue($table_="dlz_product_countrytype",$storeid_sku_value);

            }
            if($density == $optionkey){
            //2017/11/1
              $firstproduct['P1']['density'] = $this->getoptionvalue($table_="dlz_product_density",$storeid_sku_value);
              /**密度变为可选项 功能过渡判断*/
              if(empty($firstproduct['P1']['density']) || $firstproduct['P1']['density'] == ''){
                $firstproduct['P1']['density'] = $rows_product['density'];//密度
              }
            }
            if ($color == $optionkey) {
              //2019.04.13
             $firstproduct['P1']['color'] = $this->getoptionvalue($table_="dlz_product_color",$storeid_sku_value);
              /**颜色变为可选项 功能过渡判断*/
              if(empty($firstproduct['P1']['color']) || $firstproduct['P1']['color'] == ''){
                $firstproduct['P1']['color'] = $rows_product['color'];//
              }

            }
           
          }
          echo "< p1 insert >\n";
          if (!empty($firstproduct)) {
            $this->insertsql($zuzhuangarrary,$get_param=$firstproduct,$category,$country_type_value,$item_id);
          }
          
          $secondproduct = array();
          foreach ($option_arr as $optionkey2 => $optionvalue2) {
            //产品二
            $storeid_sku_value = $store_id.'-'.$sku.'-'.$optionvalue2;
              if (!$isDuotiao) {
                  $Weft = $rows_product['Weft_inc_option_id'];
              }else{
                   $Weft = $rows_product['length_option_id'];
              }
            
            if (empty($Weft)) {
             continue;
            }
            $country_type = $rows_product['country_type_option_id'];
            $color = $rows_product['color_option_id'];//2019/04/13
            if($Weft == $optionkey2){
              if (!$isDuotiao) {
                 $secondproduct['P2']['weaving'] = $this->getoptionvalue($table_="dlz_product_weftinc",$storeid_sku_value);
              }else{
                 $duotiaolength2 = $this->getoptionvalue($table_="dlz_product_length",$storeid_sku_value);
                 $duo_inc_arr2 = explode('&',$duotiaolength2);//是否是3+1 拆分
                 $str_duo2 = $duo_inc_arr2[0];//获取字符串
                 $secondproduct['P2']['weaving'] = $str_duo2;
              }
             
              $lotNum = '1';
              $secondproduct['P2']['lotNum'] = $lotNum;//打包条数
            }

            if($country_type == $optionkey2 && !empty($Weft)) {
              $secondproduct['P2']['type_country'] = $this->getoptionvalue($table_="dlz_product_countrytype",$storeid_sku_value);

            }
            if ($color == $optionkey2) {
              //2019.04.13
             $secondproduct['P2']['color'] = $this->getoptionvalue($table_="dlz_product_color",$storeid_sku_value);
              /**颜色变为可选项 功能过渡判断*/
              if(empty($secondproduct['P2']['color']) || $secondproduct['P2']['color'] == ''){
                $secondproduct['P2']['color'] = $rows_product['color'];//
              }

            }

            echo "<Weft:".$Weft.         $storeid_sku_value.       $optionkey2.">\n";
          }
          echo "< p2 insert >\n";
          if (empty($firstproduct) && empty($secondproduct)) {
            echo "数据为空退出\n";
            return false;
          }
          if (!empty($secondproduct)) {
            $this->insertsql($zuzhuangarrary,$get_param=$secondproduct,$category,$country_type_value,$item_id);
          }
           
           $this->zuzhuangPostChild($orderid=$increment_id);


 echo "++++++++++++++++++end++++++++++++++++++++\n"  ; 
 return true;
}
/**
 * [getoptionvalue 通过组合的sku 获取相应的value]
 * @param  [type] $table_            [description]
 * @param  [type] $storeid_sku_value [description]
 * @return [type]                    [description]
 */
public function getoptionvalue($table_,$storeid_sku_value){
    $sql_optionvalue = "select * from $table_ where storeid_sku='$storeid_sku_value'";
    $query_optionvalue = $this->db->query($sql_optionvalue);//返回影响了多少行数据
      while($rows_optionvalue = $query_optionvalue->fetch()){
            $optionvalue_title = $rows_optionvalue['title'];
      }
      return $optionvalue_title;
}

    /**
     * [insertsql 分别按照 单个 多个子订单进行插入]
     * @param $zuzhuangarrary
     * @param $get_param
     * @param $category
     * @param $country_type_value
     * @param $item_id
     * @internal param $ [type] $zuzhuangarrary     [description]
     * @internal param $ [type] $get_param          [description]
     * @internal param $ [type] $category           [description]
     * @internal param $ [type] $db                 [description]
     * @internal param $ [type] $country_type_value [description]
     * @internal param $ [type] $item_id            [description]
     */
public function insertsql($zuzhuangarrary,$get_param,$category,$country_type_value,$item_id){
  print_r($get_param);
   if(isset($get_param['P1'])){
    $zuzhuangarrary['child_id'] = $zuzhuangarrary['increment_id'].'-'.$item_id.'-'.'01';
    $zuzhuangarrary['orderit_itemid'] = $zuzhuangarrary['increment_id'].'-'.$item_id.'-'.'01';
    $type_lacesize = $zuzhuangarrary['laceSize'];
    foreach($get_param['P1'] as $key_p => $value_p){
           //处理类目，类型 ,蕾丝
          if($key_p == "closure" || $key_p == "Stretched"){
            $zuzhuangarrary['inch'] = trim($value_p);
          }else{
            $zuzhuangarrary[$key_p] = trim($value_p);
          }
          if($type_lacesize == "Full" || $type_lacesize == "Full Lace Frontal Wig"){
            $zuzhuangarrary['laceSize'] = 'Full lace';
            $zuzhuangarrary['category'] = "Wigs";
            $zuzhuangarrary['laceWigType'] = "Full Lace Wigs";
          }elseif($category == "Hair Weft with Closure" && $type_lacesize == "360"){
            $zuzhuangarrary['category'] = "Lace Front";
            $zuzhuangarrary['type'] = "360 Lace Frontal";
          }elseif($category == "Wig" && $type_lacesize == "360" || $type_lacesize =="360 Lace Frontal Wig"){
            $zuzhuangarrary['laceWigType'] = "360 lace wig"; 
            $zuzhuangarrary['category'] = "Wigs";
          }elseif($category == "Wig" && $type_lacesize == "360 Transparent"){
            $zuzhuangarrary['laceWigType'] = "Transparent 360 lace wig"; 
            $zuzhuangarrary['category'] = "Wigs";
          }elseif($type_lacesize == "4\" x 4\""){
            if(!empty($zuzhuangarrary['length_option_id'])){
                 $zuzhuangarrary['category'] = "Closure";
                 $zuzhuangarrary['type'] = "Lace Closure";
            }else{
                $zuzhuangarrary['category'] = "Wigs";
                $zuzhuangarrary['laceWigType'] = "4*4 lace wig";
            }
          }elseif($type_lacesize == "4\" x 13\""){
            $zuzhuangarrary['category'] = "Lace Front";
            $zuzhuangarrary['type'] = "Lace Front";
          }elseif(empty($type_lacesize)){
            $zuzhuangarrary['category'] = "Hair Weaving";
          }elseif ($type_lacesize == "3\" x 12\""){
            $zuzhuangarrary['category'] = "Wigs";
            $zuzhuangarrary['laceWigType'] = "Lace Front wigs";
          }elseif ($type_lacesize == "6*13 Transparent") {
            $zuzhuangarrary['category'] = "Wigs";
            $zuzhuangarrary['laceWigType'] = "Transparent 6*13 lace wig";
          }elseif ($type_lacesize == "6\" x 13\"") {
            $zuzhuangarrary['category'] = "Wigs";
            $zuzhuangarrary['laceWigType'] = "6*13 lace wig";
          }elseif ($category == "Hair Closure" && $type_lacesize == "360" || trim($type_lacesize) == "360 Lace Frontal") {
            $zuzhuangarrary['category'] = "Lace Front";
            $zuzhuangarrary['type'] = "360 Lace Frontal";
          }elseif ($type_lacesize == "6\" x 13\" bob") {
            $zuzhuangarrary['category'] = "Wigs";
            $zuzhuangarrary['laceWigType'] = "6*13 lace bob wig";
          }elseif ($type_lacesize == "bob 360 wig") {
            $zuzhuangarrary['category'] = "Wigs";
            $zuzhuangarrary['laceWigType'] = "bob 360 wig";
          }elseif ($type_lacesize == "bob full lace wig") {
            $zuzhuangarrary['category'] = "Wigs";
            $zuzhuangarrary['laceWigType'] = "bob full lace wig";
          }elseif ($type_lacesize == 'T part wig') {
            $zuzhuangarrary['category'] = "Wigs";
            $zuzhuangarrary['laceWigType'] = "T part wig";
          }elseif ($type_lacesize == '5*5 lace wig') {
            $zuzhuangarrary['category'] = "Wigs";
            $zuzhuangarrary['laceWigType'] = "5*5 lace wig";
          }
          if(empty($zuzhuangarrary['type_country'])){
              $zuzhuangarrary['type_country'] = $country_type_value;
          }

    }
    //print_r($zuzhuangarrary);
    echo "\n";
    unset($zuzhuangarrary['length_option_id']);
    $result = $this->DB->Getinsert($arrData=$zuzhuangarrary,$table='dlz_order_product',$sync_data="0");
    //给定处理过的产品，用 1 标示；
    if($result == '0'){
          $syncstatus_sql = "update dlz_attribute_all_id set sync_status='1' where item_id='$item_id'";
          $this->db->exec($syncstatus_sql);
    }

    //
   }elseif (isset($get_param['P2'])) {
    $zuzhuangarrary['child_id'] = $zuzhuangarrary['increment_id'].'-'.$item_id.'-'.'02';
    $zuzhuangarrary['orderit_itemid'] = $zuzhuangarrary['increment_id'].'-'.$item_id.'-'.'02';
         foreach($get_param['P2'] as $key_p1 => $value_p1){
          echo "$key_p1******\n";
          if($key_p1 == "weaving"){
            $zuzhuangarrary['inch'] = trim($value_p1);
            $zuzhuangarrary['category'] = "Hair Weaving";
            //$zuzhuangarrary['lotNum'] = $value_p1;
            unset($zuzhuangarrary['laceSize']); 
          }else{
            $zuzhuangarrary[$key_p1] = $value_p1;
          }
          if(empty($zuzhuangarrary['type_country'])){
              $zuzhuangarrary['type_country'] = $country_type_value;
          }
    }
     //print_r($zuzhuangarrary);
     echo "\n";
     unset($zuzhuangarrary['length_option_id']);
      $result = $this->DB->Getinsert($arrData=$zuzhuangarrary,$table='dlz_order_product',$sync_data="0");//
          //给定处理过的产品，用 1 标示；
      if($result == '0'){
          $syncstatus_sql = "update dlz_attribute_all_id set sync_status='1' where item_id='$item_id'";
          $this->db->exec($syncstatus_sql);
    }
   }//


}

    /**
     * [zuzhuangPostChild 组装需要推送的子订单信息]
     * @param  string $orderid [description]
     */
public function zuzhuangPostChild($orderid=''){
$sql_order = "select * from dlz_order_product where option_status='0' and increment_id='$orderid'";
$query_order = $this->db->query($sql_order);//返回影响了多少行数据
while($row_order = $query_order->fetch()){
      $sql_arrary = array();
      $sql_arrary['increment_id'] = $increment_id = $row_order['increment_id'];
      $child_id = $row_order['child_id'];
      $sql_arrary['product_id'] = $row_order['product_id'];
      $sql_arrary['category'] = $row_order['category'];
      $sql_arrary['status'] = $row_order['status'];
      $sql_arrary['type_id'] = $row_order['type_id'];
      $sql_arrary['created_at'] = $row_order['created_at'];
      $sql_arrary['updated_at'] = $row_order['updated_at'];
      $sql_arrary['weight'] = $row_order['weight'];
      $inch = $row_order['inch'];
      $sql_arrary['color'] = $row_order['color'];
      $sql_arrary['grade'] = $row_order['grade'];
      $sql_arrary['texture'] = $row_order['texture'];
      $sql_arrary['package'] = $row_order['package'];
      $sql_arrary['type_country'] = $row_order['type_country'];
      $sql_arrary['hairDesign'] = $row_order['hairDesign'];
      $sql_arrary['type'] = $row_order['type'];
      $sql_arrary['laceWigType'] = $row_order['laceWigType'];
      $sql_arrary['density'] = $row_order['density'];
      $sql_arrary['capSize'] = $row_order['capSize'];
      $sql_arrary['laceSize'] = $row_order['laceSize'];
      $sql_arrary['lotNum'] = $row_order['lotNum'];
      $sql_arrary['productCount'] = $row_order['productCount'];
      $sql_arrary['amount'] = $row_order['amount'];
      $sql_arrary['currencyCode'] = $row_order['currencyCode'];
      $orderit_itemid = $row_order['orderit_itemid'];

      $inc_arr = explode(' ',$inch);
      $countnum = count($inc_arr);
      if ("$countnum" > "1") {
        echo "大于1 ：$inch  -";

      for($index=0;$index<$countnum;$index++) 
        {
          $sql_arrary['inch'] = $inc_arr[$index];//尺寸
          $sql_arrary['orderit_itemid'] = $orderit_itemid.'-'.$index;//索引
          $sql_arrary['child_id'] = $child_id.'-'.$index;//尺寸
          $result = $this->DB->Getinsert($arrData=$sql_arrary,$table='dlz_post_orderinfo',$sync_data="0");
          //如果分解打包尺寸的订单成功，则记录状态
          if("$result" == '0'){
              $syncstatus_sql = "update dlz_order_product set option_status='1' where child_id='$child_id'";
              $this->db->exec($syncstatus_sql);
          }

        }
      }else{
        echo "小于1 ：$inch";
          $sql_arrary['inch'] = $inch;//尺寸
          $sql_arrary['orderit_itemid'] = $orderit_itemid;//索引
          $sql_arrary['child_id'] = $child_id;//尺寸
          $result = $this->DB->Getinsert($arrData=$sql_arrary,$table='dlz_post_orderinfo',$sync_data="0");
          //如果分解打包尺寸的订单成功，则记录状态
          if("$result" == '0'){
              $syncstatus_sql = "update dlz_order_product set option_status='1' where child_id='$child_id'";
              $this->db->exec($syncstatus_sql);
          }
          
      }
        $upd_sql = "update dlz_order_info set issendqm='1' where increment_id='$increment_id'";
        $this->db->exec($upd_sql);      
}
}
//提取数字
function findNum($str=''){
  $str=trim($str);
  if(empty($str)){return '';}
  $temp=array('1','2','3','4','5','6','7','8','9','0');
  $result='';
  for($i=0;$i<strlen($str);$i++){
  if(in_array($str[$i],$temp)){
  $result.=$str[$i];
  }
  }
  return $result;
}

}
